//
//  HMStatusRetweetView.swift
//  WeiBo
//
//  Created by Apple on 16/9/21.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

//  转发视图
class HMStatusRetweetView: UIView {
    
    //  转发视图的底部约束
    var retweetViewBottomConstraint: Constraint?
    
    
    var statusViewModel: HMStatusViewModel? {
        didSet {
            //  转发微博
            contentLabel.text = statusViewModel?.status?.retweeted_status?.text
            
            //  判断是否有配图
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls , picUrls.count > 0 {
                //  设置配图信息
                pictureView.picUrls = picUrls
                
                
                self.snp_updateConstraints(closure: { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HMHomeTableViewCellMargin).constraint
                })
                pictureView.isHidden = false
            } else {
                //  没有配图
                self.snp_updateConstraints(closure: { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(HMHomeTableViewCellMargin).constraint
                })
                pictureView.isHidden = true
            }
            
        }
    }
    
    //  转发微博内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.lightGray, fontSize: 13)
        label.numberOfLines = 0
        return label
    }()
    //  配图
    private lazy var pictureView: HMStatusPictureView = {
        let view = HMStatusPictureView()
        return view
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
        //  设置背景色
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        //  添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        //  设置约束
        contentLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(self).offset(HMHomeTableViewCellMargin)
            make.top.equalTo(self).offset(HMHomeTableViewCellMargin)
            make.width.equalTo(ScreenWidth - 2 * HMHomeTableViewCellMargin)
        }
        pictureView.snp_makeConstraints { (make) in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HMHomeTableViewCellMargin)
            
        }
        
        //  关键约束
        self.snp_makeConstraints { (make) in
            retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HMHomeTableViewCellMargin).constraint
        }
        
    }
    
    

}
